package mayer.Dice;

import java.util.Random;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class ShakeDice extends Activity {
        private static final int SHAKE_THRESHOLD = 15000;

        private ImageButton ib_dice1;
        private ImageButton ib_dice2;
        private ImageButton ib_dice3;
        private ImageButton ib_dice4;
        private ImageButton ib_dice5;
        private TextView textView;

        private Resources res;
        private Drawable[] dices;
        private ImageButton[] ib_dices;

        private SensorManager sManager;

        private long lastResultTime;
        private long lastTime = 0;
        private long curTime = 0;
        private long duration = 0;
        private float last_x = 0.0f;
        private float last_y = 0.0f;
        private float last_z = 0.0f;
        private float shake = 0.0f;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                textView = (TextView) findViewById(R.id.textview);
                ib_dice1 = (ImageButton) findViewById(R.id.ib_dice1);
                ib_dice2 = (ImageButton) findViewById(R.id.ib_dice3);
                //ib_dice3 = (ImageButton) findViewById(R.id.ib_dice2);
                //ib_dice4 = (ImageButton) findViewById(R.id.ib_dice4);
                //ib_dice5 = (ImageButton) findViewById(R.id.ib_dice5);
                res = getResources();
                dices = new Drawable[] { res.getDrawable(R.drawable.dice1),
                                res.getDrawable(R.drawable.dice2),
                                res.getDrawable(R.drawable.dice3),
                                res.getDrawable(R.drawable.dice4),
                                res.getDrawable(R.drawable.dice5),
                                res.getDrawable(R.drawable.dice6) };
                ib_dices = new ImageButton[] { ib_dice1, ib_dice2, ib_dice3, ib_dice4,
                                ib_dice5 };
                for (ImageButton ib : ib_dices) {
                        ib.setOnClickListener(onDiceButtonClickListener);
                }

                sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
                Sensor accelerometerSensor = sManager
                                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
                sManager.registerListener(accelerometerSensorEventListener,
                                accelerometerSensor, SensorManager.SENSOR_DELAY_UI);
        }

        @Override
        protected void onDestroy() {
                // TODO Auto-generated method stub
                sManager.unregisterListener(accelerometerSensorEventListener);
                super.onDestroy();
        }

        private void ShowResult() {
                Random random = new Random();
                for (int i = 0; i < 5; i++) {
                        ib_dices[i].setImageDrawable(dices[random.nextInt(6)]);
                }
                textView.setText("You rolled: ");

        }

        private SensorEventListener accelerometerSensorEventListener = new SensorEventListener() {

                @Override
                public void onSensorChanged(SensorEvent event) {
                        // TODO Auto-generated method stub
                        synchronized (this) {

                                
                                curTime = System.currentTimeMillis();
                                if (curTime - lastResultTime < 2000) {
                                        return;
                                }
                               
                                if ((curTime - lastTime) > 100) {
                                        duration = (curTime - lastTime);
                                      
                                        float x = event.values[SensorManager.DATA_X];
                                        float y = event.values[SensorManager.DATA_Y];
                                        float z = event.values[SensorManager.DATA_Z];

                                        shake = (Math.abs(x - last_x) + Math.abs(y - last_y) + Math
                                                        .abs(z - last_z)) / duration * 10000;
                                        Log.d("sensor", "shake detected w/ speed: " + shake);
                                        if (shake > SHAKE_THRESHOLD) {
                                                lastResultTime = curTime;
                                                ShowResult();
                                        }

                                        last_x = x;
                                        last_y = y;
                                        last_z = z;
                                        lastTime = curTime;
                                }
                        }
                }

                @Override
                public void onAccuracyChanged(Sensor sensor, int accuracy) {
                        // TODO Auto-generated method stub

                }
        };

        private OnClickListener onDiceButtonClickListener = new OnClickListener() {

                @Override
                public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getApplicationContext(), "Something1",
                                        Toast.LENGTH_LONG).show();
                }
        };

}